package com.example.shop.util;


import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件工具类
 *
 * @author huang.Mr
 * @version 1.8
 */
public class MailUtil {
    /**
     * @param Sender    发送人
     * @param Recipient 收件人
     * @param id        参数
     * @param succeed   邮件内容
     * @param url       邮件中跳转的连接
     */
    public static void getMaiUtil(String Sender, String Recipient, Object id, String succeed, String url) {
        ApplicationContext applicationContext = SpringUtil.getApplicationContext();  // 获得容器对象
        JavaMailSender bean = applicationContext.getBean(JavaMailSender.class);  // 获得对象，通过反射获得spring管理的对象
        MimeMessage mimeMessage = bean.createMimeMessage();  // 创建MimeMessage对象
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom(Sender);
            helper.setSubject("宠物乐园审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText(succeed + url + id, true);
            helper.setTo(Recipient);
            // 6.发送邮件
            bean.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}
